<template>
  <view >
    <my-search></my-search>
    <view class="cate">
      <scroll-view class="left" scroll-y :style="{height:wh+'px'}" >
        <view class="left_item" 
        @click="btnActive(index)" 
        :class="{active:index===idx}" 
        v-for="(item,index) in cateList" 
        :key="item.cat_id">{{item.cat_name}}</view>
      </scroll-view>
      <scroll-view class="right" scroll-y :style="{height:wh+'px'}" :scroll-top="scrollTop" >
        <view v-for="item in cateLevel2" :key="item.cat_id">
          <view class="title">/{{item.cat_name}}/</view>
          <view class="cateLevel3box">
            <view class=cateLevel3 v-for="sub in item.children" :key="sub.cat_id" @click="toGoodsList(sub.cat_name)">
              <image :src="sub.cat_icon" mode="widthFix"></image>
              <text>{{sub.cat_name}}</text>
            </view>
          </view>
        </view>
      </scroll-view>
    </view>
  </view>
</template>

<script>
  export default {
    data() {
      return {
        wh:0 , // 窗口可用高度
        idx:0, // 控制选中
        cateList:[], // 分类列表
        cateLevel2:[], // 右侧分类数据
        scrollTop:0 , // 距离顶部的距离
      };
    },
    methods:{
      // 三级分类点击事件
      toGoodsList(value){
        uni.navigateTo({
          url:`/pages/goods_list/goods_list?query=${value}`
        })
      },
      
      // 控制选中类型
     btnActive(index){
       this.idx=index
       this.cateLevel2=this.cateList[index].children
       this.scrollTop=this.scrollTop===0?1:0
     },
     
     // 获取分类数据
     async getCateList(){
        const {data:res}=await uni.$http.get('/api/public/v1/categories')
       if(res.meta.status!==200)return uni.$showMsa()
       this.cateList=res.message
      this.cateLevel2=this.cateList[0].children
     }
    },
    onLoad(){
      // 获取当前系统的信息
      const sysInfo=uni.getSystemInfoSync()
      // console.log(sysInfo);
      // 为wh窗口可用高度动态赋值
      this.wh=sysInfo.windowHeight-56
      
      // 获取分类列表数据
      this.getCateList()
    },
    
  }
</script>

<style lang="scss">
.cate{
  display: flex;
  .left{
    width: 120px;
    .left_item{
      height: 60px;
      line-height: 60px;
      text-align: center;
      background-color: #f4f4f4;
    }
    .active{
      background-color: #fff;
      position: relative;
      &::before{
        content: '';
        display: block;
        width: 3px;
        height: 40px;
        background-color: #eb4450;
        position: absolute;
        top:50%;
        left: 0%;
        transform: translateY(-50%);
      }
    }
  }
}
.right{
  .title{
    text-align: center;
    font-weight: 700;
    margin: 20px;
  }
  .cateLevel3box{
    display: flex;
    flex-wrap: wrap;
    .cateLevel3{
      width: 33.33%;
      display: flex;
      flex-direction: column;
      align-items: center;
      margin-top: 10px;
      image{
        width: 70%;
      }
      text{
        font-size: 12px;
      }
    }
  }
}
</style>
